<template>
  <!-- 有商品时显示 -->
  <view class="cart-container" v-if="cart.length !== 0">
    <!-- 收货地址 -->
    <my-address></my-address>
    <!-- 购物车商品列表的标题区域 -->
    <view class="cart-title">
      <uni-icons type="shop" size="18"></uni-icons>
      <text class="cart-title-text">购物车</text>
    </view>
    <!-- 商品列表区域 -->
    <uni-swipe-action>
      <block v-for="(item ,i) in cart" :key="i">
        <uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler(item)">
          <my-goods :goods="item" :showRadio="true" :showNum="true" @radio-change='radioChangeHandler'
            @num-change='numberChangeHandler'></my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    <!-- 使用自定义的结算组件 -->
    <my-settle></my-settle>
  </view>
  <!-- 无商品时显示 -->
  <view class="empty-cart" v-else>
    <image src="../../static/cart_empty@2x.png" class="empty-img"></image>
    <text class="tip-text">空空如也~~</text>
    <button size="mini" @click="gotoTabbarHome">去逛逛</button>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    data() {
      return {
        // 滑动配置
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#dd524d'
          }
        }]
      };
    },
    mixins: [badgeMix],
    computed: {
      // 从vuex中拿到cart数组
      ...mapState('m_cart', ['cart'])
    },
    methods: {
      // 从vuex中拿到方法
      ...mapMutations('m_cart', ['updateGoodsState', 'updateGoodsCount', 'removeGoodsById']),
      // 商品的勾选状态改变方法
      radioChangeHandler(item) {
        this.updateGoodsState(item)
      },
      // 商品数量发生改变
      numberChangeHandler(item) {
        console.log(item)
        this.updateGoodsCount(item)
      },
      // 滑动删除处理事件
      swipeActionClickHandler(item) {
        console.log(item);
        this.removeGoodsById(item.goods_id)

      },
      // 跳转tabbar的home页面
      gotoTabbarHome(){
        uni.switchTab({
          url:'/pages/home/home'
        })
      }
    }

  }
</script>

<style lang="scss">
  // 有商品样式
  .cart-container {
    padding-bottom: 100rpx;
  }

  .cart-title {
    height: 80rpx;
    font-size: 28rpx;
    padding-left: 10rpx;
    border-bottom: 2rpx solid #efefef;

    .cart-title-text {
      margin-left: 20rpx;
    }
  }
  // 空购物车提示样式
  .empty-cart{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding-top:300rpx ;
    .empty-img{
      width: 250rpx;
      height: 250rpx;
    }
    text{
      font-size: 24rpx;
      margin-top: 10rpx;
      color: gray;
    }
    button{
      margin-top: 10rpx;
      background-color: #ff0b60;
      color: #ffffff;
      border-radius: 50rpx;
    }
  }
</style>
